package com.sdkj.utils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author sdkj
 * 
 */
public class CommonUtils implements LoggerUtils {
	/**
	 * 获得request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得response
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * session中存放数据
	 * 
	 * @param sessionAttrName
	 *            变量的名字
	 * @param obj
	 */
	public static void setSessionAttribute(String sessionAttrName, Object obj) {
		ServletActionContext.getRequest().getSession()
				.setAttribute(sessionAttrName, obj);
	}

	/**
	 * 获取session中的数据
	 * 
	 * @param sessionAttrName
	 *            变量的名字
	 * @return 变量值
	 */
	public static Object getSessionAttribute(String sessionAttrName) {
		return ServletActionContext.getRequest().getSession()
				.getAttribute(sessionAttrName);
	}

	/**
	 * 
	 * 移除session中的数据
	 * 
	 * @param sessionAttrName
	 *            变量的名字
	 */
	public static void removeSessionAttribute(String sessionAttrName) {
		ServletActionContext.getRequest().getSession()
				.removeAttribute(sessionAttrName);
	}

	/**
	 * 注销session
	 */
	public static void removeSession() {
		ServletActionContext.getRequest().getSession().invalidate();
	}

	/**
	 * 获取值栈
	 * 
	 * @return
	 */
	public static ValueStack getValueStack() {
		// return (ValueStack)getRequest().getAttribute("struts.valueStack");
		return ActionContext.getContext().getValueStack();
	}

	/**
	 * 获得请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {
		String requestPath = request.getRequestURI();
		requestPath = requestPath
				.substring(request.getContextPath().length() + 1);// 去掉项目路径
		return requestPath;
	}

	// /**
	// * 获取当前后台用户信息
	// * @author sdkj
	// *
	// * @param attributeName 属性名 默认 currentUser
	// * @return
	// */
	// public static User getCurrentUser(String attributeName){
	// if("".equals(attributeName)||null==attributeName){
	// attributeName="currentUser";
	// }
	// return (User) getSessionAttribute(attributeName);
	// }
	//
	/**
	 * 将 json 写回 前台
	 * 
	 * @author sdkj
	 * @param json
	 *            json 串
	 * @throws IOException
	 */
	public static void writeJson(Object json) {
		getResponse().setContentType("text/html;charset=utf-8");
		try {
			getResponse().getWriter().write(json + "");
			getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String createUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
	}

	public static String transMapToString(Map map) {
		java.util.Map.Entry entry;
		StringBuffer sb = new StringBuffer();
		for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
			entry = (java.util.Map.Entry) iterator.next();
			sb.append(entry.getKey().toString())
					.append("'")
					.append(null == entry.getValue() ? "" : entry.getValue()
							.toString()).append(iterator.hasNext() ? "^" : "");
		}
		return sb.toString();
	}
}
